
<template>
<div class="home-container">
  <div class="header">
    <span>{{ username }}</span>
    <LoginOutlined style="margin: 0 10px"/>
    <span @click="onLogout">退出登录</span>
  </div>

  <div class="swiper">
    <a-carousel autoplay arrows>
      <template #prevArrow>
        <div class="custom-slick-arrow" style="left: 10px; z-index: 1">
          <left-circle-outlined />
        </div>
      </template>
      <template #nextArrow>
        <div class="custom-slick-arrow" style="right: 10px">
          <right-circle-outlined />
        </div>
      </template>
      <div style="font-weight: bold;font-size: 60px;color: #ffffff">
        学生会文件管理系统
      </div>
      <a-image
          height="300px"
          :preview="false"
          src="https://gw.alipayobjects.com/zos/antfincdn/cV16ZqzMjW/photo-1473091540282-9b846e7965e3.webp"
      />
      <a-image
          height="300px"
          :preview="false"
          src="https://gw.alipayobjects.com/zos/antfincdn/x43I27A55%26/photo-1438109491414-7198515b166b.webp"
      />
    </a-carousel>
  </div>

  <div class="content">
    <a-card hoverable style="width: 31%" @click="onClickCard('1')">
      <template #cover>
        <img class="example" src="@/assets/活动.png" />
      </template>
      <a-card-meta title="活动发布">
      </a-card-meta>
    </a-card>
<!--    <a-card hoverable style="width: 23%" @click="onClickCard('2')">-->
<!--      <template #cover>-->
<!--        <img class="example" src="@/assets/上传.png" />-->
<!--      </template>-->
<!--      <a-card-meta title="材料上传">-->
<!--      </a-card-meta>-->
<!--    </a-card>-->
    <a-card hoverable style="width: 31%" @click="onClickCard('3')">
      <template #cover>
        <img class="example" src="@/assets/下载.png" />
      </template>
      <a-card-meta title="材料下载">
      </a-card-meta>
    </a-card>
    <a-card hoverable style="width: 31%" @click="onClickCard('4')">
      <template #cover>
        <img class="example" src="@/assets/生成.png" />
      </template>
      <a-card-meta title="材料生成">
      </a-card-meta>
    </a-card>

    <div style="width: 100%">
      <div class="title">活动列表</div>
      <a-table :dataSource="dataSource" :columns="columns">
        <template #bodyCell="{ column, text, record }">
          <template v-if="column.dataIndex === 'operation'">
            <a-button type="primary" @click="onFileUpload(record)">材料上传</a-button>
            <a-popconfirm
                v-if="dataSource.length"
                title="确认删除?"
                ok-text="Yes" cancel-text="No"
                @confirm="onDeleteActive(record)"
            >
              <a-button style="margin-left: 10px" danger >删除</a-button>
            </a-popconfirm>
          </template>
          <template v-if="column.dataIndex === 'cehua_doc'">
           <div v-if="record.cehua_doc" style="display: flex;align-items: center">
             <img src="@/assets/word.png" alt="" style="width: 16px;height: 16px">
             <a-button type="link" :href="record.cehua_doc">策划案.doc</a-button>
           </div>
          </template>
          <template v-if="column.dataIndex === 'news_doc'">
            <div v-if="record.news_doc" style="display: flex;align-items: center">
              <img src="@/assets/word.png" alt="" style="width: 16px;height: 16px">
              <a-button type="link" :href="record.news_doc">新闻稿.doc</a-button>
            </div>
          </template>
          <template v-if="column.dataIndex === 'img_1'">
            <a-image
                v-if="record.img_1"
                :width="50"
                :src="record.img_1"
            />
            <a-image
                v-if="record.img_2"
                :width="50"
                :src="record.img_2"
            />
            <a-image
                v-if="record.img_3"
                :width="50"
                :src="record.img_3"
            />
          </template>
          <template v-if="column.dataIndex === 'xuanchuan_img'">
            <a-image
                v-if="record.xuanchuan_img"
                :width="50"
                :src="record.xuanchuan_img"
            />
          </template>
        </template>
      </a-table>
    </div>
  </div>

  <a-modal v-model:open="onOpenPublish" title="活动发布">
    <a-form
        ref="formRef1"
        :model="publishFormRef"
        name="basic"
        :label-col="{ span: 8 }"
        :wrapper-col="{ span: 16 }"
        autocomplete="off"
    >
      <a-form-item
          label="活动名称"
          name="name"
          :rules="[{ required: true, message: 'Please input your name!' }]"
      >
        <a-input v-model:value="publishFormRef.name" />
      </a-form-item>
      <a-form-item  label="活动策划"
                    :rules="[{ required: true, message: 'Please upload your file!' }]"
                    name="cehua_doc">
        <a-upload v-model:file-list="publishFormRef.fileList" accept=".doc,.docx" action="/api/upload/file" @change="cehuaDocUpload">
          <a-button>
            上传活动策划
          </a-button>
        </a-upload>
      </a-form-item>
    </a-form>

    <template #footer>
      <a-button type="primary" html-type="submit" @click="onSubmitPublish">确认</a-button>
    </template>
  </a-modal>
  <a-modal v-model:open="fileUpload" title="材料上传">
    <a-form
        ref="formRef2"
        :model="fileUploadForm"
        name="basic"
        :label-col="{ span: 8 }"
        :wrapper-col="{ span: 16 }"
        autocomplete="off"
    >
      <a-form-item
          label="新闻稿"
          name="news_doc"
      >
        <a-upload accept=".doc,.docx" action="/api/upload/file" @change="newsDocUpload">
          <a-button>
            上传活动策划
          </a-button>
        </a-upload>

      </a-form-item>
      <a-form-item  label="现场照片1">
        <a-upload
            v-model:file-list="imgList.img_1"
            accept="*/image" action="/api/upload/file"
            list-type="picture-card"
            @change="imgDocUpload1"
        >
          <div v-if="!fileUploadForm.img_1">
            <plus-outlined />
            <div style="margin-top: 8px">上传</div>
          </div>
        </a-upload>
      </a-form-item>
      <a-form-item  label="现场照片2">
        <a-upload
            v-model:file-list="imgList.img_2"
            accept="*/image" action="/api/upload/file"
            list-type="picture-card"
            @change="imgDocUpload2"
        >
          <div v-if="!fileUploadForm.img_2">
            <plus-outlined />
            <div style="margin-top: 8px">上传</div>
          </div>
        </a-upload>
      </a-form-item>
      <a-form-item  label="现场照片3">
        <a-upload
            v-model:file-list="imgList.img_3"
            accept="*/image" action="/api/upload/file"
            list-type="picture-card"
            @change="imgDocUpload3"
        >
          <div v-if="!fileUploadForm.img_3">
            <plus-outlined />
            <div style="margin-top: 8px">上传</div>
          </div>
        </a-upload>
      </a-form-item>
      <a-form-item  label="宣传照片">
        <a-upload
            v-model:file-list="imgList.xuanchuan_img"
            accept="*/image" action="/api/upload/file"
            list-type="picture-card"
            @change="imgDocUpload4"
        >
          <div v-if="!fileUploadForm.xuanchuan_img">
            <plus-outlined />
            <div style="margin-top: 8px">上传</div>
          </div>
        </a-upload>
      </a-form-item>
    </a-form>

    <template #footer>
      <a-button type="primary" html-type="submit" @click="onConfirmFileUpload">保存</a-button>
    </template>
  </a-modal>
  <a-modal v-model:open="fileDownload" title="材料下载" width="1200px">
    <a-table :columns="columns2" :data-source="dataSource2" :expand-column-width="100" defaultExpandAllRows>
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'action'">
          <a-button type="primary" size="mini" @click="downloadAll(record)">全部下载</a-button>
        </template>
      </template>
      <template #expandedRowRender="{ record }">
        <a-form v-for="(item,index) in record.childrens" :key="item.id">
          <div>
           {{ index + 1}} 、活动：{{ item.name }}
          </div>
          <a-form-item label="策划案">
            <div v-if="item.cehua_doc" style="display: flex;align-items: center">
              <img src="@/assets/word.png" alt="" style="width: 16px;height: 16px">
              <a-button type="link" :href="item.cehua_doc">策划案.doc</a-button>
            </div>
          </a-form-item>
          <a-form-item label="新闻稿">
            <div v-if="item.news_doc" style="display: flex;align-items: center">
              <img src="@/assets/word.png" alt="" style="width: 16px;height: 16px">
              <a-button type="link" :href="item.news_doc">新闻稿.doc</a-button>
            </div>
          </a-form-item>
          <a-form-item label="现场照片">
            <div style="display: flex;align-items: center">
              <div  v-if="item.img_1" style="display: flex;flex-direction: column;justify-content: center;align-items: center;width: 100px">
                <a-image
                    :width="50"
                    :src="item.img_1"
                />
                <a-button type="link" @click="downloadFile(item.img_1,item.name + '_' + '现场照片1.png')">点击下载</a-button>
              </div>
              <div  v-if="item.img_2" style="display: flex;flex-direction: column;justify-content: center;align-items: center;width: 100px;margin-left: 10px">
                <a-image
                    :width="50"
                    :src="item.img_2"
                />
                <a-button type="link" @click="downloadFile(item.img_2,item.name + '_' + '现场照片2.png')">点击下载</a-button>
              </div>
              <div  v-if="item.img_3" style="display: flex;flex-direction: column;justify-content: center;align-items: center;width: 100px;margin-left: 10px">
                <a-image
                    :width="50"
                    :src="item.img_3"
                />
                <a-button type="link" @click="downloadFile(item.img_3,item.name + '_' + '现场照片3.png')">点击下载</a-button>
              </div>
            </div>
          </a-form-item>
          <a-form-item label="宣传图片">
            <div  v-if="item.xuanchuan_img" style="display: flex;flex-direction: column;justify-content: center;align-items: center;width: 100px;margin-left: 10px">
              <a-image
                  :width="50"
                  :src="item.xuanchuan_img"
              />
              <a-button type="link" @click="downloadFile(item.xuanchuan_img,item.name + '_' + '宣传照片.png')">点击下载</a-button>
            </div>
          </a-form-item>
          <a-divider />
        </a-form>
      </template>
    </a-table>
  </a-modal>
</div>
</template>
<script setup>
import {ref} from "vue";
import {LoginOutlined, LeftCircleOutlined, RightCircleOutlined, PlusOutlined} from "@ant-design/icons-vue";
import {useRouter} from "vue-router";
import axios from "axios";
import {message} from "ant-design-vue";
import {downloadFile} from "@/utils";
const router = useRouter()
const username = ref(localStorage.getItem('username') );
const dataSource = ref([])
const columns = ref([
  {
    title: '活动名称',
    dataIndex: 'name',
    key: 'name',
  },
  {
    title: '创建人',
    dataIndex: 'member.username',
    key: 'member.username',
    customRender({ record}) {
      return record.member.username
    }
  },
  {
    title: '活动策划',
    dataIndex: 'cehua_doc',
  },
  {
    title: '新闻稿',
    dataIndex: 'news_doc',
  },
  {
    title: '现场照片',
    dataIndex: 'img_1',
  },
  {
    title: '宣传图片',
    dataIndex: 'xuanchuan_img',
  },
  {
    title: '创建时间',
    dataIndex: 'create_time',
    key: 'create_time',
  },
  {
    title: '操作',
    dataIndex: 'operation',
  },
])
const getActiveList = () => {
  axios.get('/api/active/list')
      .then(res => {
        if (res.data.code !== 200) {
          message.error(res.data.msg);
        } else {
          console.log('response', res.data);
          dataSource.value = res.data.data
        }
      })
      .catch(error => {
        console.log('error', error);
      });
}
getActiveList()
const onLogout = () => {
  router.replace({
    path: '/login'
  })
}
const onClickCard = (id) => {
  console.log(id)
  if (id === '1') {
    onOpenPublish.value = true
  } else if (id === '3') {
    axios.get('/api/member/level').then(res => {
      if (res.data.code !== 200) {
        message.error(res.data.msg);
      } else {
        dataSource2.value = res.data.data
        fileDownload.value = true
      }
    })
  }
}
const fileDownload = ref(false)
const dataSource2 = ref([])
const columns2 = ref([
  {
    title: '用户名称',
    dataIndex: 'name',
    key: 'name',
  },
  {
    title: '操作',
    dataIndex: 'action',
  },
])
const onOpenPublish = ref(false)
const publishFormRef = ref({
  name: '',
  cehua_doc: '',
  fileList: []
})
const formRef1 = ref(null)
const onSubmitPublish = () => {
  formRef1.value
      .validate()
      .then(() => {
        axios.post('/api/active', {
          ...publishFormRef.value,
          fileList: undefined,
          user_id: Number(localStorage.getItem('userId'))
        })
            .then(res => {
              if (res.data.code !== 200) {
                message.error(res.data.msg);
              } else {
                message.success("新增成功");
                console.log('response', res.data);
                onOpenPublish.value = false
                getActiveList()
              }
            })
            .catch(error => {
              console.log('error', error);
            });
      })
      .catch(error => {
        console.log('error', error);
      });
}
const cehuaDocUpload = (e) => {
  console.log(e)
  if (e.file.status === 'done') {
    publishFormRef.value.cehua_doc = e.fileList[0].response.data.url
  }
}

/**
 * 材料上传
 */
const formRef2 = ref(null)
const fileUpload = ref(false)
const fileUploadForm = ref({
  id: '',
  news_doc: '',
  xuanchuan_img: '',
  img_1: '',
  img_2: '',
  img_3: '',
})
const imgList = ref({
  news_doc: [],
  img_1: [],
  img_2: [],
  img_3: [],
  xuanchuan_img: []
})
const onFileUpload = (record) => {
  fileUploadForm.value.id = record.id
  fileUploadForm.value.news_doc = record.news_doc
  fileUploadForm.value.img_1 = record.img_1
  fileUploadForm.value.img_2 = record.img_2
  fileUploadForm.value.img_3 = record.img_3
  fileUploadForm.value.xuanchuan_img = record.xuanchuan_img
  imgList.value = {
    news_doc: [{
      thumbUrl: record.news_doc
    }].filter(e => e.thumbUrl),
    img_1: [{
      url: record.img_1
    }].filter(e => e.url),
    img_2: [{
      url: record.img_2
    }].filter(e => e.url),
    img_3: [{
      url: record.img_3
    }].filter(e => e.url),
    xuanchuan_img: [{
      url: record.xuanchuan_img
    }].filter(e => e.url)
  }
  fileUpload.value = true
}
// 删除
const onDeleteActive = (record) => {
  axios.delete('/api/active/' + record.id).then(res => {
    if (res.data.code !== 200) {
      message.error(res.data.msg);
    } else {
      message.success("删除成功");
      getActiveList()
    }
  })
}
const onConfirmFileUpload = () => {
  console.log(fileUploadForm)
  formRef2.value
      .validate()
      .then(() => {
        axios.put('/api/active/' + fileUploadForm.value.id, {
          news_doc: fileUploadForm.value.news_doc,
          img_1: fileUploadForm.value.img_1,
          img_2: fileUploadForm.value.img_2,
          img_3: fileUploadForm.value.img_3,
          xuanchuan_img: fileUploadForm.value.xuanchuan_img
        })
            .then(res => {
              if (res.data.code !== 200) {
                message.error(res.data.msg);
              } else {
                message.success("上传成功");
                console.log('response', res.data);
                getActiveList()
                fileUpload.value = false
              }
            })
            .catch(error => {
              console.log('error', error);
            });
      })
      .catch(error => {
        console.log('error', error);
      });
}
const newsDocUpload = (e) => {
  console.log(e)
  if (e.file.status === 'done') {
    fileUploadForm.value.news_doc = e.fileList[0].response.data.url
  }
}
// 现场照片1 2 3
const imgDocUpload1 = (e) => {
  console.log(e)
  if (e.file.status === 'done') {
    fileUploadForm.value.img_1 = e.fileList[0].response.data.url
  }
  if(e.file.status === 'removed') {
    fileUploadForm.value.img_1 = ''
  }
}
const imgDocUpload2 = (e) => {
  if (e.file.status === 'done') {
    fileUploadForm.value.img_2 = e.fileList[0].response.data.url
  }
  if(e.file.status === 'removed') {
    fileUploadForm.value.img_2 = ''
  }
}
const imgDocUpload3 = (e) => {
  console.log(e)
  if (e.file.status === 'done') {
    fileUploadForm.value.img_3 = e.fileList[0].response.data.url
  }
  if(e.file.status === 'removed') {
    fileUploadForm.value.img_3 = ''
  }
}
// 宣传照片
const imgDocUpload4 = (e) => {
  console.log(e)
  if (e.file.status === 'done') {
    fileUploadForm.value.xuanchuan_img = e.fileList[0].response.data.url
  } else if (e.file.status === 'removed') {
    fileUploadForm.value.xuanchuan_img = ''
  }
}

const downloadAll = (record) => {
  let filePath = []
  record.childrens.forEach(el => {
    filePath.push(el.cehua_doc)
    filePath.push(el.news_doc)
    filePath.push(el.img_1)
    filePath.push(el.img_2)
    filePath.push(el.xuanchuan_img)
  })
  filePath = filePath.filter(e => e)
  axios.post('/api/download/all', filePath)
}
</script>

<style scoped lang="less">
.home-container {
  width: 100%;
  min-height: 100%;
  background: #f0f2f5 url('~@/assets/background.ebcb9160.svg') no-repeat 50%;
  background-size: 100%;
  position: relative;
  :deep(.ant-card-cover) {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 200px;
    .example {
      width: 50%;
    }
  }
  :deep(.ant-card-meta-title) {
    text-align: center;
  }
  .content {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    padding: 60px 0;
    justify-content: space-between;
  }
  .title {
    line-height: 60px;
    font-size: 24px;
    font-weight: bold;
  }
  .header {
    position: relative;
    height: 64px;
    padding: 0 40px;
    background: #fff;
    -webkit-box-shadow: 0 1px 4px rgba(0,21,41,.08);
    box-shadow: 0 1px 4px rgba(0,21,41,.08);
    display: flex;
    align-items: center;
    flex-direction: row;
    justify-content: flex-end;
    cursor: pointer;
  }
  :deep(.slick-slide) {
    text-align: center;
    height: 300px;
    line-height: 300px;
    background: #364d79;
    overflow: hidden;
    font-weight: bold;
    font-size: 60px;
    color: #ffffff;
  }
  :deep(.slick-arrow.custom-slick-arrow) {
    width: 25px;
    height: 25px;
    font-size: 25px;
    color: #fff;
    background-color: rgba(31, 45, 61, 0.11);
    transition: ease all 0.3s;
    opacity: 0.3;
    z-index: 1;
}
  :deep(.slick-arrow.custom-slick-arrow:before) {
    display: none;
  }
  :deep(.slick-arrow.custom-slick-arrow:hover) {
    color: #fff;
    opacity: 0.5;
  }
}
</style>